package com.diandian.backend.dingding.data.service;

import com.diandian.backend.dingding.data.domain.DingdingStaffWithDeptInfo;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.dingding.data.domain.DingdingStaff;
import com.diandian.backend.dingding.data.dto.*;
import com.diandian.backend.dingding.data.mapper.DingdingStaffMapper;
import com.diandian.backend.dingding.data.resp.DingdingStaffDetailResp;
import com.diandian.backend.dingding.data.resp.DingdingStaffPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.dingding.data.resp.DingdingStaffListResp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DingdingStaffService {

    @Autowired
    private DingdingStaffMapper dingdingStaffMapper;

    public PageResult page(DingdingStaffPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = dingdingStaffMapper.getCount(requestDTO);

        // 查询数据集合
        List<DingdingStaffWithDeptInfo> dataList = dingdingStaffMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<DingdingStaffPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                DingdingStaffPageResp item = new DingdingStaffPageResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setUserId(x.getUserId());
                item.setUnionId(x.getUnionId());
                item.setName(x.getName());
                item.setAvatar(x.getAvatar());
                item.setStateCode(x.getStateCode());
                item.setMobile(x.getMobile());
                item.setTelephone(x.getTelephone());
                item.setJobNumber(x.getJobNumber());
                item.setTitle(x.getTitle());
                item.setEmail(x.getEmail());
                item.setOrgEmail(x.getOrgEmail());
                item.setWorkPlace(x.getWorkPlace());
                item.setRemark(x.getRemark());
                item.setHiredDate(x.getHiredDate());
                item.setActive(x.getActive());
                item.setAdmin(x.getAdmin());
                item.setBoss(x.getBoss());
                item.setLeader(x.getLeader());
                item.setExclusiveAccount(x.getExclusiveAccount());
                item.setDeptCode(x.getDeptCode());
                item.setDeptName(x.getDeptName());
                item.setStar(x.getStar());
                item.setXinghuoTier(x.getXinghuoTier());
                item.setRecommendBrand(x.getRecommendBrand());
                item.setStatus(x.getStatus());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<DingdingStaffListResp> getList(DingdingStaffListParamDTO requestDTO){

        // 查询数据集合
        List<DingdingStaff> dataList = dingdingStaffMapper.getList(requestDTO);

        // 组装数据
        List<DingdingStaffListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                DingdingStaffListResp item = new DingdingStaffListResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setUserId(x.getUserId());
                item.setUnionId(x.getUnionId());
                item.setName(x.getName());
                item.setAvatar(x.getAvatar());
                item.setStateCode(x.getStateCode());
                item.setMobile(x.getMobile());
                item.setTelephone(x.getTelephone());
                item.setJobNumber(x.getJobNumber());
                item.setTitle(x.getTitle());
                item.setEmail(x.getEmail());
                item.setOrgEmail(x.getOrgEmail());
                item.setWorkPlace(x.getWorkPlace());
                item.setRemark(x.getRemark());
                item.setHiredDate(x.getHiredDate());
                item.setActive(x.getActive());
                item.setAdmin(x.getAdmin());
                item.setBoss(x.getBoss());
                item.setLeader(x.getLeader());
                item.setExclusiveAccount(x.getExclusiveAccount());
                respList.add(item);
            });
        }

        return respList;
    }

    public DingdingStaffDetailResp getDetail(DingdingStaffDetailParamDTO param) {

        DingdingStaff dbData = dingdingStaffMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        DingdingStaffDetailResp result = new DingdingStaffDetailResp();
        result.setId(dbData.getId());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        result.setUserId(dbData.getUserId());
        result.setUnionId(dbData.getUnionId());
        result.setName(dbData.getName());
        result.setAvatar(dbData.getAvatar());
        result.setStateCode(dbData.getStateCode());
        result.setMobile(dbData.getMobile());
        result.setTelephone(dbData.getTelephone());
        result.setJobNumber(dbData.getJobNumber());
        result.setTitle(dbData.getTitle());
        result.setEmail(dbData.getEmail());
        result.setOrgEmail(dbData.getOrgEmail());
        result.setWorkPlace(dbData.getWorkPlace());
        result.setRemark(dbData.getRemark());
        result.setHiredDate(dbData.getHiredDate());
        result.setActive(dbData.getActive());
        result.setAdmin(dbData.getAdmin());
        result.setBoss(dbData.getBoss());
        result.setLeader(dbData.getLeader());
        result.setExclusiveAccount(dbData.getExclusiveAccount());
        return result;
    }

    public void updateById(DingdingStaffUpdateByIdParamDTO param) {
        param.check();

        DingdingStaffDetailParamDTO queryDetailDTO = new DingdingStaffDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        DingdingStaff dbData = dingdingStaffMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        dingdingStaffMapper.updateById(param);
    }

    public void batchUpdateByUserId(List<DingdingStaffUpdateByIdParamDTO> dataList){
        if(dataList == null || dataList.size() == 0){
            return;
        }

        dataList.stream().forEach( x ->{
            if(x.getUserId() == null || "".equals(x.getUserId())){
                throw new DefaultException("用户ID不能为空");
            }
            if(x.getBelong() == null || "".equals(x.getBelong())){
                throw new DefaultException("归属标识不能为空");
            }
            x.setUpdateTime(new Date());
        });

        dingdingStaffMapper.batchUpdateByUserId(dataList);
    }

    public void deleteById(DingdingStaffDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        dingdingStaffMapper.deleteById(paramDTO);
    }

    public void add(DingdingStaffAddParamDTO param) {
        dingdingStaffMapper.add(param);
    }

    public void batchAdd(List<DingdingStaffAddParamDTO> dataList) {

        if(dataList == null || dataList.size() == 0){
            return;
        }

        for (DingdingStaffAddParamDTO dto : dataList) {
            dto.setDel(0);
            dto.setCreateTime(new Date());
        }

        dingdingStaffMapper.batchAdd(dataList);
    }
}
